package com.homegoods.orderinfodetail.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.homegoods.orderinfodetail.R;
import com.homegoods.orderinfodetail.bean.AddressListModel;
import com.homegoods.orderinfodetail.databinding.InfoActivityConfirmOrderBinding;
import com.homegoods.orderinfodetail.item.AddressManagerItem;
import com.homegoods.orderinfodetail.model.AddressModel;
import com.homegoods.orderinfodetail.presenter.ConfirmOrderPresenter;
import com.homegoods.orderinfodetail.ui.fragment.TipDialog;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.orderinfodetail.ui.view.ConfirmOrderView;
import com.homegoods.resource.SpacesItemDecoration;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.mvp.BaseView;
import com.kedll.library.util.MyUtils;
import com.kedll.library.util.ToastUtil;

/**
 * 确认订单
 * @author QiQi
 */
@Route(path = ARouterPath.PATH_CONFIRM_ORDER)
public class ConfirmOrderActivity extends MyBaseFragmentActivity<InfoActivityConfirmOrderBinding, ConfirmOrderPresenter> implements ConfirmOrderView {
    private String contents;
    private String price;
    private String dayang;
    private RecyclerView recyclerView;

    private AddressModel mAddress;
    private AddressListModel addressListModel;

    @Override
    public int getLayout() {
        return R.layout.info_activity_confirm_order;
    }

    @Override
    protected void initEvent() {
        mBinding.setPresenter(mPresenter);

        //购买须知
        onClicked(mBinding.tvSubmit, o -> new TipDialog()
                .setTitle("购买须知")
                .setMsg("1、由于购买的家具规格是1000x1000×1000平方米，此商品需要电梯空间不得小于1000X1000×1000平方米，请用户测量之后在下单，避免出现不必要的麻烦，出现此类原因卖家拒绝退货.\n" +
                        "2、商品到货后请仔细验货，如非商品质量原因，卖家拒绝退货。")
                .showDialog(getSupportFragmentManager(), "tipDialog"));

        //添加地址
        onClicked(mBinding.tvAddAddress, o -> ARouter.getInstance()
                .build(ARouterPath.PATH_ADDRESS_MANAGER).withBoolean("isOrder", true)
                .navigation(this, 1010));

        //配送方式

        //发票
        onClicked(mBinding.acInvoice, o -> ARouter.getInstance()
                .build(ARouterPath.PATH_INVOICE)
                .navigation());

        mPresenter.setListData(contents);
        onClicked( mBinding.tvSubmit,o -> {
            if (addressListModel != null) { //add 'if & else' shibo.zheng
                mPresenter.getOrderDetails(addressListModel.getId(), price);
            } else {
                ToastUtil.showToast("请正确填写收货地址");
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mBinding.setPresenter(mPresenter);
        recyclerView = mBinding.list;   //商品展示list

        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
//        recyclerView.setLayoutManager(MyUtils.getFlexBoxLayoutManager(this));
        recyclerView.addItemDecoration(new SpacesItemDecoration(2, 8));
        recyclerView.setAdapter(mPresenter.getAdapter());
        mBinding.acInvoiceDayang.setAcSubTitle(dayang);
        mBinding.acPrice.setAcSubTitle(price);
        mBinding.acPriceDayang.setAcSubTitle(dayang);
    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        ARouter.getInstance().inject(this);
        setTitle("确认订单");
        if (getIntent().getExtras() != null) {
            contents = getIntent().getStringExtra("contents");
            Log.d(TAG, "initToolBar: contents == " + contents);
            price = getIntent().getStringExtra("price");
            dayang = getIntent().getStringExtra("dayang");
        }
    }

    @Override
    protected ConfirmOrderPresenter getPresenter() {
        return new ConfirmOrderPresenter(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1010 && resultCode == RESULT_OK) {
//            mAddress = JSON.parseObject(data.getExtras().getString("AddressModel"), AddressModel.class);
//            showAddress(mAddress);
            addressListModel = JSON.parseObject(data.getExtras().getString("AddressModel"), AddressListModel.class);
            showAddress(addressListModel);
        }
    }

    @Override
    public void showAddress(AddressListModel addressModel) {
        mBinding.addressLl.setVisibility(View.VISIBLE);
//        mBinding.setItem(mAddress);
        mBinding.setItem(addressModel.getItem());
    }
}
